package com.wcar.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcar.common.entity.Menu;
import com.wcar.common.entity.User;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.system.dao.MenuDao;
import com.wcar.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenuDao menuDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Result<List<Menu>> getMenuByUserId() {
        String token = WebUtil.getToken();
        String s = redisTemplate.opsForValue().get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        List<Menu> menus = menuDao.selectMenus(user.getUserId());
        List<Menu> firsts = menus.stream().filter(item -> item.getParentId().equals(0)).collect(Collectors.toList());
        for (Menu f:firsts){
            f.setChildMenu(getChildren(f.getMenuId(),menus));
        }
        return new Result<>(200,"查询菜单成功",firsts);
    }

    @Override
    public Result<List<Menu>> getMenuByUser() {
        List<Menu> menus = menuDao.selectMenu();
        List<Menu> firsts = menus.stream().filter(item -> item.getParentId().equals(0)).collect(Collectors.toList());
        for (Menu f:firsts){
            f.setChildMenu(getChildren(f.getMenuId(),menus));
        }
        return new Result<>(200,"查询菜单成功",firsts);
    }

    @Override
    public Result addMenu(Menu menu) {
        int insert = menuDao.insert(menu);
        return new Result(200,insert==1?"添加成功":"没添加上(数据库问题)",null );
    }

    @Override
    public Result updateMenu(Menu menu) {
        int i = menuDao.updateById(menu);
        return new Result(200,i==1?"修改成功":"没修改上(数据库问题)",null);
    }

    @Override
    public Result deleteMenu(Integer menuId) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("menu_id",menuId);
        int delete = menuDao.delete(wrapper);
        return new Result(200,delete==1?"删除成功":"没删除(数据库问题)",null);
    }


    @Override
    public Result<List<Menu>> selectAll() {
        List<Menu> menus = menuDao.selectList(null);
        List<Menu> firsts = menus.stream().filter(item -> item.getParentId().equals(0)).collect(Collectors.toList());
        for (Menu f:firsts){
            f.setChildMenu(getChildren(f.getMenuId(),menus));
        }
        return new Result<>(200,"查询菜单成功",firsts);
    }

    private List<Menu> getChildren(Integer menuId,List<Menu> menus){
        List<Menu> children = new ArrayList<>();
        for (Menu m :menus){
            if(m.getParentId().equals(menuId)){
                children.add(m);
            }
        }
        for (Menu c:children){
            c.setChildMenu(getChildren(c.getMenuId(),menus));
        }
        return children;
    }
}
